//
//  JAOriginalStatusView.swift
//  JAWeiBo
//
//  Created by Arvin on 17/4/6.
//  Copyright © 2017年 Arvin. All rights reserved.
//

import UIKit
import SDWebImage

class JAOriginalStatusView: UIView {
    
    //视图模型
    var statusViewModel: JAStatusViewModel? {
        didSet {
            
            //设置真实数据
            let url = URL(string: (statusViewModel?.statusModel.user?.avatar_large)!)
            userIcon.sd_setImage(with: url!, placeholderImage: UIImage(named: "avatar_default_big"))
            userNameLable.text = statusViewModel?.statusModel.user?.screen_name
            statusLabel.text = statusViewModel?.statusModel.text
            sourceLable.text = statusViewModel?.sourceStr
            vipIcon.image = statusViewModel?.vipImage
            levelIcon.image = statusViewModel?.levelImage
            timeLable.text = statusViewModel?.timeStr
            
            pictureView.statusViewModel = statusViewModel
           
            //裁剪圆角头像
            SDWebImageManager.shared().loadImage(with: url, options: [], progress: nil) { (image, _, _, _, _, _) in
                image?.createCircleImage(size: CGSize(width: 35, height: 35), callBack: { (circleImage) in
                    self.userIcon.image = circleImage
             })
           }
            
            //判断是否有配图.有就显示,无就不显示
            //有配图
            if let count = statusViewModel?.statusModel.pic_urls?.count, count > 0 {
                pictureView.snp.updateConstraints({ (make) in
                    make.top.equalTo(statusLabel.snp.bottom).offset(10)
                    make.size.equalTo(statusViewModel!.pictureViewSize)
                })
                
            }
            else { //无配图
                pictureView.snp.updateConstraints({ (make) in
                    make.top.equalTo(statusLabel.snp.bottom)
                    make.size.equalTo(CGSize.zero)                })

            }
        }
    }
    
    ///用户头像
    lazy var userIcon: UIImageView = UIImageView(imageName: "avatar_default_big")
    
    ///用户的vip图标
    lazy var vipIcon: UIImageView = UIImageView(imageName: "avatar_grassroot")
    
    ///用户的昵称
    lazy var userNameLable: UILabel = UILabel(title: "我兜兜有糖")
    
    ///用户的皇冠等级图标
      lazy var levelIcon: UIImageView = UIImageView(imageName: "common_icon_membership_level6")
    
    ///微博来源
     lazy var sourceLable: UILabel = UILabel(title: "新浪", textColor: UIColor.lightGray, fontSize: 12)
    
    ///微博发布时间
     lazy var timeLable: UILabel = UILabel(title: "两小时前", textColor: UIColor.lightGray, fontSize: 12)
    
    ///微博正文label
     lazy var statusLabel: FFLabel = FFLabel(title: nil)
    
    ///配图视图
    let pictureView: JAStatusPictureView = JAStatusPictureView()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }


}


extension JAOriginalStatusView {
    fileprivate func setupUI () {
        
        //添加子控件
        addSubview(userIcon)
        addSubview(vipIcon)
        addSubview(userNameLable)
        addSubview(levelIcon)
        addSubview(sourceLable)
        addSubview(timeLable)
        addSubview(statusLabel)
        addSubview(pictureView)
        
        
        //添加约束
        userIcon.snp.makeConstraints { (make) in
            make.top.left.equalTo(self).offset(10)
            make.size.equalTo(CGSize(width: 35, height: 35))
        }
        
        vipIcon.snp.makeConstraints { (make) in
            make.left.equalTo(userIcon.snp.right).offset(-10)
            make.top.equalTo(userIcon.snp.bottom).offset(-10)
            make.size.equalTo(CGSize(width: 15, height: 15))
        }
        
        userNameLable.snp.makeConstraints { (make) in
            make.left.equalTo(userIcon.snp.right).offset(10)
            make.top.equalTo(userIcon)
        }
        
        levelIcon.snp.makeConstraints { (make) in
            make.left.equalTo(userNameLable.snp.right).offset(10)
            make.bottom.equalTo(userNameLable).offset(-3)
            make.size.equalTo(CGSize(width: 15, height: 15))
        }
        
        timeLable.snp.makeConstraints { (make) in
            make.left.equalTo(userIcon.snp.right).offset(10)
            make.bottom.equalTo(userIcon)
        }
        
        sourceLable.snp.makeConstraints { (make) in
            make.left.equalTo(timeLable.snp.right).offset(10)
            make.bottom.equalTo(userIcon)
        }
        
        statusLabel.snp.makeConstraints { (make) in
            make.top.equalTo(userIcon.snp.bottom).offset(10)
            make.left.equalTo(userIcon)
            make.right.equalTo(self).offset(-10)
        }

        //首次创建cell时的布局, cell只创建一次, 后面被重用时, cell的配图的约束应该由数据决定
        pictureView.snp.makeConstraints { (make) in
            make.top.equalTo(statusLabel.snp.bottom).offset(10)
            make.left.equalTo(userIcon)
            make.bottom.equalTo(self).offset(-10)
            make.size.equalTo(CGSize.zero)
        }
        

        
    }
}
